<?php
	$dstLinkPost="index.php?id=organisateur_liste_joueurs";
	$localHomeLink="index.php?id=organisateur_liste_joueurs";
?>

<?php if(!isset($_POST['pseudo'])){ ?>

<form method="POST" action="<?php echo $dstLinkPost; ?>">
	<label for="login">Consulter un joueur</label>
	 <select name="pseudo">
        <?php 	  
			$vSql = "SELECT DISTINCT pseudo, nom, prenom from participation natural join membre";
			$vQuery = pg_query($oSql, $vSql);
			while($vResult = pg_fetch_array($vQuery)) {
				echo '<option value="'.$vResult['pseudo'].'">'.$vResult['prenom'].' '.$vResult['nom'].' ('.$vResult['pseudo'].')</option>';
			}
		 ?>
     </select>
    <input type="submit" />
 </form>
 <?php echo isset($_GET['update'])?'<form action="#" method="post">':''; ?>
<h1 align="center">Listes des joueurs </h1>
<table style="width: 100%; margin: 10px auto 20px auto;">
	<tr>
		<th class="palette palette-firm">Pseudo</th>
		<th class="palette palette-firm">Nom</th>
		<th class="palette palette-firm">Prenom</th>		
		<th class="palette palette-firm" colspan="2">Action</th>
	</tr>
	
	<?php
		$vSql = "select distinct pseudo,nom, prenom from participation natural join membre where participation.pseudo = membre.pseudo order by pseudo";
		$vQuery = pg_query($oSql, $vSql);
		while($vResult = pg_fetch_array($vQuery)) {
		
			if (isset($_GET['update']) && $_GET['update'] == $vResult['pseudo'])
			{	
				echo '<tr>';
				echo '  <td class="palette palette-clouds" style="text-align: center;"><input type="text" value="' . $vResult['pseudo'] . '" name="pseudo" /></td>';
				echo '  <td class="palette palette-clouds" style="text-align: center;"><input type="text" value="' . $vResult['nom'] . '" name="nom" /></td>';
				echo '  <td class="palette palette-clouds" style="text-align: center;"><input type="text" value="' . $vResult['prenom'] . '" name="prenom" /></td>';
				echo '  <td class="palette palette-clouds" style="text-align: center;"><a href="index.php?id=organisateur_liste_joueurs&delete=' . $vResult['pseudo'] . '">Supprimer</a></td>';
				echo '  <td class="palette palette-clouds" style="text-align: center;" ><input type="submit" value="Valider" /></td>';
				echo '</tr>';
				
			}
			else{
				echo '<tr>';
				echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['pseudo'] . '</td>';
				echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['nom'] . '</td>';
				echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['prenom'] . '</td>';
				echo '  <td class="palette palette-clouds" style="text-align: center;"><a href="index.php?id=organisateur_liste_joueurs&delete=' . $vResult['pseudo'] . '">Supprimer</a></td>';
				echo '  <td class="palette palette-clouds" style="text-align: center;"><a href="index.php?id=organisateur_liste_joueurs&update=' . $vResult['pseudo'] . '">Modifier</a></td>';
				echo '</tr>';
			}
		}
	?>
</table>
<?php echo isset($_GET['update'])?'</form>':''; ?>
<h4> Ajouter un joueur </h4>
<a href="index.php?id=organisateur_ajout_joueur"> cliquez ici</a>
<?php
}else if(isset($_POST['pseudo']) && !isset($_POST['edition'])){?>

<form method="POST" action="<?php echo $dstLinkPost; ?>">
<label for="login">Selectionnez l'édition</label>
<select name="edition">
<?php 	  
	$vSql_ = "SELECT edition from participation where pseudo='".$_POST['pseudo']."'";
	$vQuery_ = pg_query($oSql, $vSql_);
	while($vResult_ = pg_fetch_array($vQuery_)) {
		echo '<option value="'.$vResult_['edition'].'">'.$vResult_['edition'].'</option>';
	}
 ?>
</select>
<input type="hidden" name="pseudo" value="<?php echo $_POST['pseudo']; ?>" /> 
<input type="submit"/>
</form>
<?php
}
else{
	$vSql = "SELECT DISTINCT edition from participation where pseudo='".$_POST['pseudo']."'ORDER BY edition";
	$vQuery = pg_query($oSql, $vSql); 
	while($vResult3 = pg_fetch_array($vQuery)) {
	
		$vSql2 = "SELECT DISTINCT * from participation natural join membre where pseudo='".$_POST['pseudo']."' limit 1";
		$vQuery2 = pg_query($oSql, $vSql2);
		while($vResult2 = pg_fetch_array($vQuery2)) {
			echo '<h1 align="center"> information du joueur '.$vResult2['prenom'].' '.$vResult2['nom'].' ('.$vResult2['pseudo'].')</h1><br/>';
			echo "<p>";
			
			echo '<h3>informations membre : </h3>';
			echo '<b>Adresse : </b> '.$vResult2['adresse_numero'].' '.$vResult2['adresse_rue'].' '.$vResult2['adresse_cpostal'].' '.$vResult2['adresse_ville'].'<br/>';
			echo '<b>Telephone : </b> '.$vResult2['telephone'].' <br/>';
			
			if($vResult2['apasseeliminatoire'] == false){
				echo '<b> Ce joueur n\'a pas pass&eacute; la phase &eacute;liminatoire </b><br/>';
			}else{
				echo '<b> Ce joueur a pass&eacute; la phase &eacute;liminatoire </b><br/>';
				echo '<b>Classement : </b> '.$vResult2['classement'].'<br/>';
				
				//echo '<h2>informations sur les matchs :</h2>';
				//afficheRound($vResult2['round1'],"1",$oSql);
				//afficheRound($vResult2['round2'],"2",$oSql);
				//afficheRound($vResult2['round3'],"3",$oSql);
				
			}		
			
			
				echo '<h2 align="center"> Edition '.$_POST['edition'].' </h2>';
				$vSql = "SELECT sum(quantite) as nb from quantite where edition='".$_POST['edition']."' and pseudo='".$_POST['pseudo']."'";
				$vQuery = pg_query($oSql, $vSql);
				$vResult = pg_fetch_array($vQuery);
				echo '<h2>Composition du deck ('.$vResult['nb'].' cartes) </h2>';
				echo '<h3> Ressource </h3>';
				?>
				<table style="width: 100%; margin: 10px auto 30px auto;">
					<tr>
						<th class="palette palette-firm">Carte</th>
						<th class="palette palette-firm">Extension</th>
						<th class="palette palette-firm">Point ressource</th>
						<th class="palette palette-firm">Faculte</th>
						<th class="palette palette-firm">Quantite</th>
					</tr>
					<?php
						$vSql = "SELECT * from vressource natural join quantite where edition='".$_POST['edition']."' and pseudo='".$_POST['pseudo']."'";
						$vQuery = pg_query($oSql, $vSql);
						while($vResult = pg_fetch_array($vQuery)) {
							echo '<tr>';
							echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['nomcarte'] . '</td>';
							echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['nomextension'] . '</td>';
							echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['pointressources'] . '</td>';
							echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['faculte'] . '</td>';
							echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['quantite'] . '</td>';
							echo '</tr>';
						}
					?>
				</table>
				<?php
				echo '<h3> Invention </h3>';
				?>
				<table style="width: 100%; margin: 10px auto 30px auto;">
					<tr>
						<th class="palette palette-firm">Carte</th>
						<th class="palette palette-firm">Extension</th>
						<th class="palette palette-firm">Potentiel d'attaque</th>
						<th class="palette palette-firm">Potentiel de defense</th>
						<th class="palette palette-firm">Cout</th>
						<th class="palette palette-firm">Faculte</th>
						<th class="palette palette-firm">Quantite</th>
					</tr>
					<?php
						$vSql = "SELECT * from vinvention natural join quantite where edition='".$_POST['edition']."' and pseudo='".$_POST['pseudo']."'";
						$vQuery = pg_query($oSql, $vSql);
						while($vResult = pg_fetch_array($vQuery)) {
							echo '<tr>';
							echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['nomcarte'] . '</td>';
							echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['nomextension'] . '</td>';
							echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['potentielattaque'] . '</td>';
							echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['potentieldefense'] . '</td>';
							echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['cout'] . '</td>';
							echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['faculte'] . '</td>';
							echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['quantite'] . '</td>';
							echo '</tr>';
						}
					?>
				</table>
				
				<?php
				echo '<h3> Effet </h3>';
				?>
				<table style="width: 100%; margin: 10px auto 30px auto;">
					<tr>
						<th class="palette palette-firm">Carte</th>
						<th class="palette palette-firm">Extension</th>
						<th class="palette palette-firm">Cout</th>
						<th class="palette palette-firm">Faculte</th>
						<th class="palette palette-firm">Quantite</th>
					</tr>
					<?php
						$vSql = "SELECT * from veffets natural join quantite where edition='".$_POST['edition']."' and pseudo='".$_POST['pseudo']."'";
						$vQuery = pg_query($oSql, $vSql);
						while($vResult = pg_fetch_array($vQuery)) {
							echo '<tr>';
							echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['nomcarte'] . '</td>';
							echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['nomextension'] . '</td>';
							echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['cout'] . '</td>';
							echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['faculte'] . '</td>';
							echo '  <td class="palette palette-clouds" style="text-align: center;">' . $vResult['quantite'] . '</td>';
							echo '</tr>';
						}
					?>
				</table>
				<form method="POST" action="<?php echo $dstLinkPost; ?>">
				<label for="login">Autres participations du joueur </label>
				<select name="edition">
				<?php 	  
					$vSql_ = "SELECT edition from participation where pseudo='".$_POST['pseudo']."'";
					$vQuery_ = pg_query($oSql, $vSql_);
					while($vResult_ = pg_fetch_array($vQuery_)) {
						echo '<option value="'.$vResult_['edition'].'">'.$vResult_['edition'].'</option>';
					}
				 ?>
				</select>
				<input type="hidden" name="pseudo" value="<?php echo $_POST['pseudo']; ?>" /> 
				<input type="submit"/>
				</form>
				<?php
				
			echo "</p>";
		}	
	}
}

echo '<br/><a href="'.$localHomeLink.'" > retour </a>';
?>
<?php


?>



